package offer;

public class
Offer16 {


    public double myPow(double x, int n) {
        double res=1;
        if (x==0){
            return  0;
        }

        long b=n;
        if (b==0){
            return 1;
        }
        if (b<0){
            x=1/x;
//            todo: 变为-b
//          res= myPow(x,-b);
            b=-b;
        }

        while (b>0){
            if ((b&1)==1){
                res*=x;
            }
            x*=x;
            b>>=1;
        }
        return  res;

    }


    public double myPow1(double x, int n) {
        if(x==0)
            return 0;
        long b=n;
        double res = 1.0;
        if(b<0){
            x=1/x;
            b=-b;
        }
        while(b>0){
            if((b&1)==1)
                res*=x;
            x*=x;
            b>>=1;
        }
        return res;
    }

}
